package com.tyk.bookstore.back.user.service.impl;


import com.tyk.bookstore.back.common.core.model.constant.OssConstant;
import com.tyk.bookstore.back.common.core.util.AssertUtil;
import com.tyk.bookstore.back.common.core.util.TreeUtil;
import com.tyk.bookstore.back.common.other.oss.manager.OssManager;
import com.tyk.bookstore.back.common.security.util.PasswordUtil;
import com.tyk.bookstore.back.common.security.util.SecurityUtil;
import com.tyk.bookstore.back.system.model.dto.RouteDto;
import com.tyk.bookstore.back.system.model.query.UpdateUserInfoQuery;
import com.tyk.bookstore.back.system.model.result.UserResultEnum;
import com.tyk.bookstore.back.system.service.UserApiService;
import com.tyk.bookstore.back.user.model.query.UpdateBaseInfoQuery;
import com.tyk.bookstore.back.user.model.query.UpdatePasswordQuery;
import com.tyk.bookstore.back.user.model.vo.RouteVo;
import com.tyk.bookstore.back.user.model.vo.UserInfoVo;
import com.tyk.bookstore.back.user.service.UserCenterService;
import com.tyk.bookstore.back.user.util.MapstructCenter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class UserCenterServiceImpl implements UserCenterService {

    private final OssManager ossManager;
    private final UserApiService userApiService;
    private final MapstructCenter mapstructCenter;

    @Override
    public List<RouteVo> getRoutes() {
        List<RouteDto> dtoList = userApiService.selectRouteDtoList(SecurityUtil.getLoginId());
        return TreeUtil.buildTree(
                dtoList,
                RouteDto::getId,
                RouteDto::getParentId,
                mapstructCenter::toGetRouteVo,
                RouteVo::addChild
        );
    }

    @Override
    public UserInfoVo getUserInfo() {
        return mapstructCenter.toUserInfoVo(userApiService.selectUserInfoVo(SecurityUtil.getLoginId()));
    }

    @Override
    public void uploadAvatar(MultipartFile avatar) {
        String url = ossManager.upload(avatar, OssConstant.USER_AVATAR);
        UpdateUserInfoQuery query = new UpdateUserInfoQuery()
                .setId(SecurityUtil.getLoginId())
                .setAvatar(url);
        userApiService.updateUserInfo(query);
    }

    @Override
    public void updateBaseInfo(UpdateBaseInfoQuery query) {
        UpdateUserInfoQuery qr = mapstructCenter
                .toUpdateUserInfoQuery(query)
                .setId(SecurityUtil.getLoginId());
        userApiService.updateUserInfo(qr);
    }

    @Override
    public void updatePassword(UpdatePasswordQuery query) {
        String dbPwd = userApiService.selectPasswordById(SecurityUtil.getLoginId());
        AssertUtil.isTrue(
                PasswordUtil.check(query.getOldPassword(), dbPwd),
                UserResultEnum.PASSWORD_ERROR
        );
        UpdateUserInfoQuery qr = new UpdateUserInfoQuery()
                .setId(SecurityUtil.getLoginId())
                .setPassword(
                        PasswordUtil.encrypt(query.getNewPassword())
                );
        userApiService.updateUserInfo(qr);
    }

}
